import Picker from "react-native-picker";
import area from "../json/area.json";

const data = [];

export default function showAreaPicker() {
  return new Promise((resolve, reject) => {
    if (data.length === 0) {
      const len = area.length;
      for (let i = 0; i < len; i++) {
        const city = [];
        for (let j = 0, cityLen = area[i].city.length; j < cityLen; j++) {
          const _city = {};
          _city[area[i].city[j].name] = area[i].city[j].area;
          city.push(_city);
        }

        const _data = {};
        _data[area[i].name] = city;
        data.push(_data);
      }
    }
    Picker.init({
      pickerData: data,
      pickerConfirmBtnText: "确定",
      pickerCancelBtnText: "取消",
      pickerTitleText: "请选择地区",
      pickerToolBarBg: [255, 255, 255, 1],
      pickerBg: [255, 255, 255, 1],
      selectedValue: ["北京", "北京", "东城区"],
      onPickerConfirm: pickedValue => {
        resolve(pickedValue);
      },
      onPickerCancel: pickedValue => {
        reject(pickedValue);
      },
    });
    Picker.show();
  });
}
